package org.mogoo.haydn;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mogoo.haydn.tools.ConnDB;

import com.google.gson.stream.JsonReader;

public class PointServlet extends HttpServlet
{


	/**
	 * Constructor of the object.
	 */
	public PointServlet()
	{
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		ResultSet rs = null;
		Connection ct = new ConnDB().getConn();
		PreparedStatement ps = null; 
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		System.out.println("connected haydn project");
		
		//writer.print("Connected");
		//writer.print("hello");
		String json_point = request.getParameter("point");
		String json_acc_records = request.getParameter("acc");
		String json_acc_time_mode = request.getParameter("acc_time_mode");
		System.out.println(json_point);
		System.out.println("-----");
		System.out.println(json_acc_records);
		System.out.println("-----");
		System.out.println(json_acc_time_mode);
		
		
		writer.print("OK");
		System.out.println("Data received from phone!");
		System.out.println(json_point);
		long time_ = 0; 
		double lat = 0;
		double lon = 0;
		double utm_east = 0;
		double utm_north = 0;
		String utm_lon_zone = "";
		String utm_lat_zone = "";
		double speed = 0;
		double altitude = 0;
		double baering = 0;
		double accuracy = 0;
		int userid = 0;
		try
		{
			// table grid_trajectory
			JsonReader reader = new JsonReader(new StringReader(json_point));
			reader.beginArray();
			int i = 0;
			while(reader.hasNext())
			{
				reader.beginObject();
				while(reader.hasNext())
				{
					String tagName = reader.nextName();
					if(tagName.equals("time_"))
					{
						time_ = reader.nextLong();
					}
					else if(tagName.equals("lat"))
					{
						lat = reader.nextDouble();
					}
					else if(tagName.equals("lng"))
					{
						lon = reader.nextDouble();
					}
					else if(tagName.equals("utm_east"))
					{
						utm_east = reader.nextDouble();
					}
					else if(tagName.equals("utm_north"))
					{
						utm_north = reader.nextDouble();
					}
					else if(tagName.equals("utm_lon_zone"))
					{
						utm_lon_zone = reader.nextString();
					}
					else if(tagName.equals("utm_lat_zone"))
					{
						utm_lat_zone = reader.nextString();
					}
					else if(tagName.equals("speed"))
					{
						speed = reader.nextDouble();
					}
					else if(tagName.equals("baering"))
					{
						baering = reader.nextDouble();
					}
					else if(tagName.equals("accuracy"))
					{
						accuracy = reader.nextDouble();
					}
					else if(tagName.equals("altitude"))
					{
						altitude = reader.nextDouble();
					}	
					else if(tagName.equals("userid"))
					{
						userid = reader.nextInt();
					}
				}
				reader.endObject();
				
				String sql = "insert into point_ (time_, userid, lat, lon, utm_east, utm_north, utm_lon_zone, utm_lat_zone, speed, altitude, bearing, accuracy) values ( "+ time_ +", "+ userid+", "+ lat +", "+ lon +", " + utm_east + ", " + utm_north + ", '" + utm_lon_zone + "', '" + utm_lat_zone + "', " + speed + ", " + altitude + ", "+ baering +", "+ accuracy +" );" ;
				ps = ct.prepareStatement(sql);
				ps.executeUpdate();
			}
			reader.endArray();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		try
		{	//------------------------------------------------------------------------
			time_ = 0;
			double x = 0;
			double y = 0;
			double z = 0;
			userid = 0;
			int status = 0;
			
			JsonReader reader = new JsonReader(new StringReader(json_acc_records));
			reader.beginArray();
			int i = 0;
			while(reader.hasNext())
			{
				reader.beginObject();
				while(reader.hasNext())
				{
					String tagName = reader.nextName();
					if(tagName.equals("time_"))
					{
						time_ = reader.nextLong();
					}
					else if(tagName.equals("x"))
					{
						x = reader.nextDouble();
					}
					else if(tagName.equals("y"))
					{
						y = reader.nextDouble();
					}
					else if(tagName.equals("z"))
					{
						z = reader.nextDouble();
					}
					else if(tagName.equals("userid"))
					{
						userid = reader.nextInt();
					}
					else if(tagName.equals("status"))
					{
						status = reader.nextInt();
					}
				}
				reader.endObject();
				
				String sql = "insert into acc_records (time_, userid, x, y, z, status_) values ( "+ time_ +", "+ userid +", "+ x +", " + y + ", " + z + ", " + status + " );" ;
				ps = ct.prepareStatement(sql);
				ps.executeUpdate();
			}
			reader.endArray();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		try
		{	
			//-------------------------------------------------------------------------------------
			userid = 0;
			long startTime = 0;
			long endTime = 0;
			int mode = 0;
			
			JsonReader reader = new JsonReader(new StringReader(json_acc_time_mode));
			reader.beginArray();
			while(reader.hasNext())
			{
				reader.beginObject();
				while(reader.hasNext())
				{
					String tagName = reader.nextName();
					if(tagName.equals("startTime"))
					{
						startTime = reader.nextLong();
					}
					else if(tagName.equals("endTime"))
					{
						endTime = reader.nextLong();
					}
					else if(tagName.equals("mode"))
					{
						mode = reader.nextInt();
					}
					else if(tagName.equals("userid"))
					{
						userid = reader.nextInt();
					}
				}
				reader.endObject();
				String sql = "insert into acc_time_mode (userid, start_time, end_time, mode_) values ( "+ userid +", "+ startTime +", "+ endTime +", " + mode + " );" ;
				ps = ct.prepareStatement(sql);
				ps.executeUpdate();
			}
			reader.endArray();
			System.out.println("update tables complete!");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				ps.close();
				ct.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
